#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Time: 2019/4/9  22:35
# @Author: 余浪人
# @email: yulangren520@gmail.com
from flask_login import login_required
from apps.cms import cms_bp
from flask import render_template, request
from apps.forms.member_form import Member_Form, Member_Edit_Form, Member_Password_Edit_Fomr, Grade_Form
from apps.models.member_model import db, Member, Grade
from apps.lib.public import add_data, check_obj, member_pwd_edit, edit_data, fake_delete_data, real_delete_data, \
    recover_data, update_status, real_deletes, fake_deletes, log, page_auto


# 启用与停用会员_列表
@cms_bp.route('/all_member_list', endpoint='member_list', methods=['GET', 'DELETE', 'POST', 'DELETE_S'])
@login_required
def member_list():
    if request.method == 'DELETE':  # 假删除
        log('操作会员软删除')
        return fake_delete_data(Member, db, request.form.get('uid'))
    elif request.method == 'DELETE_S':
        log('操作会员批量软删除')
        uid_s = list(eval(request.form.get('data_s') + ','))
        return fake_deletes(Member, db, uid_s)
    elif request.method == 'POST':
        uid, status = request.form.get('uid'), request.form.get('status')
        return update_status(Member, db, uid, status)
    obj_s = Member.query.order_by(db.desc(Member.id)).filter(Member.status != 0)
    username = request.args.get('username')
    if username:
        obj_s = Member.query.order_by(db.desc(Member.id)).filter(Member.username == username, Member.status != 0)
    paginate, page = page_auto(obj_s)
    return render_template('sys_templates/member_list.html', title='会员列表', data_s=paginate,url='cms_bp.member_list',page=page)


# 删除会员_列表
@cms_bp.route('/del_member_list', endpoint='del_member_list', methods=['GET', 'DELETE', 'UPDATE', 'DELETE_S'])
@login_required
def delete_member_list():
    if request.method == 'DELETE':  # 彻底删除
        log('操作会员彻底删除')
        return real_delete_data(Member, db, request.form.get('uid'))
    elif request.method == 'DELETE_S':
        log('操作会员批量彻底删除')
        return real_deletes(Member, db, uid_s = list(eval(request.form.get('data_s') + ',')))
    elif request.method == 'UPDATE':
        log('操作会员恢复')
        return recover_data(Member, db, request.form.get('uid'))
    obj_s = Member.query.order_by(db.desc(Member.id)).filter(Member.status == 0)
    username = request.args.get('username')
    if username:
        obj_s = Member.query.order_by(db.desc(Member.id)).filter(Member.username == username, Member.status == 0)
    paginate, page = page_auto(obj_s)
    return render_template('sys_templates/member_list.html', title='已删除会员列表', data_s=paginate,url='cms_bp.del_member_list', page=page)


# 添加会员
@cms_bp.route('/member_add', endpoint='member_add', methods=['GET', 'POST'])
@login_required
def member_add():
    if request.method == 'POST':
        log('操作会员添加')
        return add_data(Member, Member_Form, db)
    form = Member_Form(request.form)
    return render_template('sys_templates/member.html', form=form, title='添加会员信息')


# 修改会员
@cms_bp.route('/member_edit/<int:id>', endpoint='member_edit', methods=['GET', 'POST'])
@login_required
def member_edit(id):
    if request.method == 'POST':
        log('操作会员资料修改')
        return edit_data(Member_Edit_Form, Member, db, id)
    obj = Member.query.filter_by(id=id).first()
    if not check_obj(obj):  # 回显数据
        form = Member_Edit_Form(data=dict(obj))
        return render_template('sys_templates/member.html', form=form, title='修改会员信息')


# 修改密码
@cms_bp.route('/password_edit/<int:id>', endpoint='password_edit', methods=['GET', 'POST'])
@login_required
def member_password_edit(id):
    obj = Member.query.filter_by(id=id).first()
    if request.method == 'POST':
        log('操作会员密码修改')
        return member_pwd_edit(Member, Member_Password_Edit_Fomr, db, request.form.get('password_old'), id)
    elif not check_obj(obj):  # 回显数据
        form = Member_Password_Edit_Fomr(data=dict(obj))
        return render_template('sys_templates/member.html', form=form, title='修改会员密码')


# 会员等级管理
@cms_bp.route('/grade', endpoint='grade', methods=['GET', 'DELETE', 'DELETE_S'])
@login_required
def grade_list():
    if request.method == 'DELETE':
        log('操作会员等级删除')
        return fake_delete_data(Grade, db, request.form.get('uid'))
    elif request.method == 'DELETE_S':
        log('操作会员等级批量删除')
        uid_s = list(eval(request.form.get('data_s') + ','))
        return fake_deletes(Grade, db, uid_s)
    obj_s = Grade.query.order_by(db.desc(Grade.id)).filter(Grade.status == 1)
    paginate, page = page_auto(obj_s)
    return render_template('sys_templates/grade_list.html', title='会员等级管理', data_s=paginate,url = 'cms_bp.grade', page = page)


# 会员等级添加
@cms_bp.route('/grade_add', endpoint='grade_add', methods=['GET', 'POST'])
@login_required
def grade_add():
    form = Grade_Form(request.form)
    if request.method == 'POST':
        log('操作会员等级添加')
        return add_data(Grade, Grade_Form, db)
    return render_template('sys_templates/grade.html', title='会员等级添加', form=form)


# 会员等级修改
@cms_bp.route('/grade_edit/<int:id>', endpoint='grade_edit', methods=['GET', 'POST'])
@login_required
def grade_edit(id):
    if request.method == 'POST':
        log('操作会员等级修改')
        return edit_data(Grade_Form, Grade, db, id)
    obj = Grade.query.filter_by(id=id).first()
    if not check_obj(obj):
        form = Grade_Form(data=dict(obj))
        return render_template('sys_templates/grade.html', title='会员等级修改', form=form)